package todo.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import todo.Utils;
import todo.model.Todo;

public enum Dao {
	INSTANCE;

	public void add(String userId, String summary, String description) {
		synchronized (this) {
			EntityManager em = EMFService.get().createEntityManager();
			Todo todo = new Todo(userId, summary, description);
			em.persist(todo);
			em.close();
		}
	}

	public void edit(String todoId, String summary, String description) {
		synchronized (this) 
	{
			EntityManager em = EMFService.get().createEntityManager();
			Todo todo = getTodo(todoId);
			//JDOHelper.getPersistenceManager(todo);
			todo.setLongDescription(description);
			todo.setShortDescription(summary);
			em.remove(todo);
			em.persist(todo);
			em.close();
		}
	}
	
	public List<Todo> getTodos(String pageNo, String todoId) 
	{
		List<Todo> result = new ArrayList<Todo>();
		if (Utils.isDevMode()) 
		{
			if (!Utils.isNull(todoId))
			{
				Todo t = new Todo("", "summary dev mode", "jsddfwefgmnklwenmfwe" +
						"wrtewrtwerwegtertwfwe e rtg erg erg" +
						"ertenkjrgneubrufer er fgeruger gerigje \n wfefwef" +
						"wfefnwuefbhwue fwefwef \n werwerwerwerwewfwef" +
						"fwwegf erfwg feg df gdfgwefwnfjdnf efegfmeng ferge");
				t.setId(Long.parseLong(todoId));
				result.add(t);
				return result;
			}
			else
			{
				for (long i = 1; i < 11; i++)
				{
					Todo t = new Todo("", "summary dev mode", "jsddfwefgmnklwenmfwe" +
							"wrtewrtwerwegtertwfwe e rtg erg erg" +
							"ertenkjrgneubrufer er fgeruger gerigje \n wfefwef" +
							"wfefnwuefbhwue fwefwef \n werwerwerwerwewfwef" +
							"fwwegf erfwg feg df gdfgwefwnfjdnf efegfmeng ferge");
					t.setId(i);
					result.add(t);
				}
				return result;
			}
		}
		
		if (!Utils.isNull(todoId))
		{
			Todo todo = getTodo(todoId);
			result.add(todo);
			return result;
		}

		EntityManager em = EMFService.get().createEntityManager();
		try
		{
			//Query q = em.createQuery("select t from Todo t" + (user != null ? " where t.author = :userId" : "") + " order by t.myId desc");
			Query q = em.createQuery("select t from Todo t order by t.myId desc");
			//if (user != null) {q.setParameter("userId", user);}
			List<Todo> todos = q.getResultList();
			int starting = 0;
			int pageSize = 10;
			if (pageNo !=  null && pageNo.equals("-1"))
			{
				pageSize = Integer.MAX_VALUE;
			}
			else if (pageNo != null && !pageNo.equals(""))
			{
				starting = Integer.parseInt(pageNo) * pageSize;
			}
			for (int i = (Math.min(starting, todos.size())); i < (Math.min(todos.size(),starting + pageSize)); i++)
			{
				result.add(todos.get(i));
			}
			return result;
		}
		finally {
			em.close();
		}
		
	}

	public List<Todo> searchTodos(String search, String pageNo) {
		List<Todo> result = new ArrayList<Todo>();
		List<Todo> todos = getTodos("-1", null);
		for (Todo todo : todos) 
		{
			if (todo.getShortDescription().toLowerCase().contains(search.toLowerCase()) 
					|| todo.getLongDescription().toLowerCase().contains(search.toLowerCase()))
			{
				result.add(todo);
			}
		}
		return result;
	}

	public Todo getTodo(String id) {
		if (id == null) {return null;}
		EntityManager em = EMFService.get().createEntityManager();
		try
		{
			Todo result = em.find(Todo.class, Long.parseLong(id));
			return result;	
		} finally {
			em.close();
		}
	}
	
	public void remove(long id) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			Todo todo = em.find(Todo.class, id);
			em.remove(todo);
		} finally {
			em.close();
		}
	}
	
}